package com.example.finalexamcovid_19situations;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;

public class maDaWeiCnvas extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_ma_da_wei_cnvas);

        //获取屏幕大小
        MaDaWeiDraw.Constant.point = new Point();
        getWindowManager().getDefaultDisplay().getSize(MaDaWeiDraw.Constant.point);

        //将屏幕设置为画布
        MaDaWeiDraw myView=new MaDaWeiDraw(this);
        setContentView(myView);


        Intent intent=getIntent();
        String[] date=intent.getStringArrayExtra("extra_date");
        String[] data=intent.getStringArrayExtra("extra_data");
        String title=intent.getStringExtra("extra_title");

        //传值到绘画类
        myView.SetInfo(date, // X轴刻度
                getYScale(data),    // Y轴刻度
                data,    // 数据
                title);
    }
    //获取Y轴刻度的函数
    String [] getYScale(String [] data){
        String [] YScale=new String[5];
        int maxAbs=0;
        boolean negative=false;
        //获取绝对值最大的数
        for (int i=0;i< data.length;i++){
            if (maxAbs<Math.abs(Integer.parseInt(data[i]))){
                maxAbs=Math.abs(Integer.parseInt(data[i]));
            }
            //判断是否有负数，有负数则negative为true
            if (Integer.parseInt(data[i])<0){
                negative=true;
            }
        }
        int maxYScale=maxAbs;
        //获取最大绝对值的位数的下一位
        int maxAbsDigit=String.valueOf(maxAbs).length()-1;
        //计算最大Y刻度
        while (maxYScale%Math.pow(10,maxAbsDigit)!=0){
            maxYScale++;
        }
        //判断非负数
        if (!negative){
            YScale[0]="0";
            YScale[1]=String.valueOf(maxYScale/4);
            YScale[2]=String.valueOf(maxYScale/2);
            YScale[3]=String.valueOf(maxYScale/4*3);
            YScale[4]=String.valueOf(maxYScale);
        }else {
            YScale[0]="-"+String.valueOf(maxYScale);
            YScale[1]="-"+String.valueOf((maxYScale)/2);
            YScale[2]="0";
            YScale[3]=String.valueOf((maxYScale)/2);
            YScale[4]=String.valueOf(maxYScale);
        }

        return YScale;
    }
}